<!-- 面包屑组件 -->
<template>
    <!-- 指定动画模式mode属性：out-in(先离开后进入)、in-out(先进入后离开) -->
    <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem v-if="category.top" :to="`/category/${category.top.id}`">{{ category.top.name }}</XtxBreadItem>
        <Transition name="fade-right" mode="out-in">
            <XtxBreadItem v-if="category.sub" :key="category.sub.id">{{ category.sub.name }}</XtxBreadItem>
        </Transition>
    </XtxBread>
</template>
<script>
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
import { computed } from 'vue'
export default {
    name: 'SubBread',
    setup() {
        const route = useRoute()
        const store = useStore()
        // 3. 通过计算属性得到，二级类目的名称和ID，一级类目的名称和ID
        const category = computed(() => {
            const obj = {}
            store.state.category.list.forEach(top => {
                top.children && top.children.forEach(sub => {
                    if (sub.id === route.params.id) {
                        // 设置二级类目
                        obj.sub = { id: sub.id, name: sub.name }
                        // 设置一级类目
                        obj.top = { id: top.id, name: top.name }
                    }
                })
            })
            return obj
        })
        // 模版需要使用的东西需要setup返回
        return { category }
    }
}
</script>
<style scoped lang="less">

</style>